package util.date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	/**
	 * Format String : yyyyMMddHHmmssSSS
	 */
	public static final String Format_TO_LONG_MILLIS = "yyyyMMddHHmmssSSS";
	/**
	 * Format String : yyyyMMddHHmmss
	 */
	public static final String Format_TO_LONG = "yyyyMMddHHmmss";
	/**
	 * Format String : yyyy-MM-dd HH:mm:ss
	 */
	public static final String Format_TO_STANDARD = "yyyy-MM-dd HH:mm:ss";
	/**
	 * Format String : yyyy-MM-dd
	 */
	public static final String Format_TO_SHORT_ONE = "yyyy-MM-dd";
	/**
	 * Format String : yyyyMMdd
	 */
	public static final String Format_TO_SHORT_TWO = "yyyyMMdd";
	/**
	 * Format String : HHmmss
	 */
	public static final String Format_TO_TIME = "HHmmss";

	public static String getCurrentDate(String dateFormat) {
		return new SimpleDateFormat(dateFormat).format(new Date());
	}

	public static String getDateFormat(Date date, String dateFormat) {
		return new SimpleDateFormat(dateFormat).format(date);
	}

	public static String getCurrentDateFront() {
		String strDate = new SimpleDateFormat("yyyyMMdd").format(addDays(new Date(), -1));
		strDate = strDate.substring(0, 4) + strDate.substring(4, 6) + strDate.substring(6);
		return strDate;
	}
	
	public static long getCurrentTime() {
		return Calendar.getInstance().getTimeInMillis();
	}
	
	
	public static Date addDays(Date date, int num) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(5, num);

		return c.getTime();
	}

	public static Date addMonths(Date date, int num) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(2, num);
		return c.getTime();
	}

	public static Date addYears(Date date, int num) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(1, num);
		return c.getTime();
	}

	public static Date addSeconds(Date date, int num) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(13, num);
		return c.getTime();
	}



	public static Date getDate(String dateStr, String dateFormat) throws Exception {
		SimpleDateFormat df = new SimpleDateFormat(dateFormat);
		Date cDate = null;
		try {
			cDate = df.parse(dateStr);
		} catch (Exception e) {
			throw new Exception(e);
		}
		return cDate;
	}

	public static Date convertTimerToDate(Timestamp time) {
		Date dd = new Date(time.getTime());
		return dd;
	}

	public static long DateDiff(Date date1, Date date2) {
		return date1.getTime() - date2.getTime();
	}

	public static int DateDiffDay(Date date1, Date date2) {
		int day = (int) ((date1.getTime() - date2.getTime()) / 3600000L);
		return day;
	}

	@SuppressWarnings("unused")
    public static boolean isValidDate(String dateStr, String pattern) {
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		df.setLenient(false);
		Date date = null;
		try {
			date = df.parse(dateStr);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	public static void checkValidDate(String dateStr, String pattern) throws Exception {
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		df.setLenient(false);
		try {
			df.parse(dateStr);
		} catch (Exception e) {
			throw new Exception("[" + dateStr + "]日期格式非法");
		}
	}

	public static int getDateSpace(String date1, String date2, String dateFormat) throws Exception {
		int days = 0;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
			Calendar calst = Calendar.getInstance();

			Calendar caled = Calendar.getInstance();
			calst.setTime(sdf.parse(date1));
			caled.setTime(sdf.parse(date2));

			calst.set(11, 0);
			calst.set(12, 0);
			calst.set(13, 0);
			caled.set(11, 0);
			caled.set(12, 0);
			caled.set(13, 0);

			days = ((int) (caled.getTime().getTime() / 1000L) - (int) (calst.getTime().getTime() / 1000L)) / 3600 / 24;
		} catch (Exception e) {
			throw new Exception(e);
		}
		return days;
	}

	public static String getMsgId() {
		int ran = getRandom(10);
		String msgId = getCurrentDate("yyyyMMddHHmmss") + "-" + String.valueOf(ran);
		return msgId;
	}
	
	public static int getRandom(int length) {
		int num = 1;
		double random = Math.random();
		if (random < 0.1D) {
			random += 0.1D;
		}
		for (int i = 0; i < length; i++) {
			num *= 10;
		}
		return (int) (random * num);
	}
}
